/**
 * @author wonderful 20180116
 * 选项卡面板控件
 */
(function(){

    var params={
        width:"",//宽度
        height:"",//高度
        isAutoWidth :false,// 是否自适应宽度，优先级高于width。
        isAutoHeight :true,// 是否自适应高度，优先级高于height。
        event:{
            onClosed:function(tab,content,tabObj){//选项卡面板关闭后事件

            }
        },
        tabs:[//选项卡定义
            {
                title:"",//选项卡标题
                onclick:function(tab,content,tabObj){//选项卡单击事件，参数分别为选项卡对象、内容对象、整个选项卡控件对象。

                }
            }
        ]
    };

    $.fn.extend({"wdfTab":function(method,originalOpt){

            var opt={};
            $.extend(true,opt,originalOpt);
            var tabObj=$(this);

            if(method=="init"){

                var tabBar=tabObj.find(".tabBar");
                var tabs=tabBar.find(".tab");
                var contentBar=tabObj.find(".contentBar");
                var contents=contentBar.find(".content");
                var bgPanel=$("<div class='wdfTabBgPanel'></div>").appendTo($("body"));

                var tabRightBar = tabObj.find(".tabRightBar");

                if(tabs.length>1){
                    var closeBtn=$("<div class='closeBtn'></div>").appendTo(tabRightBar);
                    tabBar.css({"float":"left","width":"auto"});
                    tabs.css({"float":"left","textAlign":"center","marginRight":"1px","width":"140px"});

                }else{
                    var closeBtn=$("<div class='closeBtn'></div>").appendTo(tabBar);
                    tabs.css({"paddingLeft":"30px"})
                }
                function initSize(){

                    var width=$(window).width();
                    var height=$(window).height();

                    tabObj.width(width);
                    tabObj.height(height);
                    tabRightBar.width(tabObj.width()-tabs.length*141);

                  /*  if(tabs.length>0){
                        contentBar.height(height-tabBar.height()-80);
                    }else{
                        contentBar.height(0);
                    }*/
                    // contents.height(contentBar.height()-2);
                    contents.not(".show").css("display","none");
                    // tabObj.css({left:"50%",top:"50%",marginLeft:-1*tabObj.width()/2+"px",marginTop:-1*tabObj.height()/2});
                    tabObj.css({left:0,top:0,width:"100%",height:"100%"});
                    var resizeFun=tabObj.data("resizeFun");
                    if(resizeFun){
                        tabObj.data("resizeFun")();
                    }else{
                        tabObj.data("resizeFun",function () {
                            //增加延迟，为了准确获取浏览器窗体可视区域尺寸。
                            setTimeout(function(){
                                var width = $(window).width();
                                var height = $(window).height();
                                tabObj.width(width);
                                tabObj.height(height);
                                // tabObj.css({left:"50%",top:"50%",marginLeft:-1*tabObj.width()/2+"px",marginTop:-1*tabObj.height()/2});
                                contentBar.height(height-tabBar.height()-60);
                                tabObj.css({left:0,top:0,width:"100%",height:"100%"});
                            },1);
                        });
                        $(window).bind("resize",tabObj.data("resizeFun"));
                    }
                    tabObj.data("resizeFun")();
                }

                initSize();

                $(tabs).each(function(i,tab){

                    var content=$(contents[i]);
                    var onClickEvent=null;

                    if(opt.tabs&&opt.tabs[i]&&opt.tabs[i].onClick){
                        onClickEvent=opt.tabs[i].onClick;
                    }

                    $(tab).bind("click",function(){
                        $("#posTitle").hide();
                        contentBar.find(".content").removeClass("show");
                        tabBar.find(".tab").removeClass("show");
                        contents.css("display","none");
                        $(tab).addClass("show");
                        content.addClass("show");
                        content.css("display","block");
                        if(onClickEvent)
                            onClickEvent(tab,content,tabObj);

                    });

                })

                closeBtn.bind("click",function(){
                    tabObj.animate({"opacity":"0"},500);
                    setTimeout(function(){
                        tabObj.css({"display":"none"});
                    },500);
                    bgPanel.hide();
                    $(".tempFileList").remove();
                    if(opt.event&&opt.event.onClosed){
                        opt.event.onClosed($(this),bgPanel,tabObj);
                    }
                });

                tabObj.show=function(){
                    bgPanel.show();
                    initSize();
                    bgPanel.css("width","100%");
                    bgPanel.css("height","100%");
                    tabObj.css({"display":"block","opacity":"0"});
                    tabObj.animate({"opacity":"1"},300);

                };

                tabObj.close=function(){
                    closeBtn.click();
                };

            }

            return tabObj;

        }

    });
})($)